package com.chuanyang.driverh.mainorder;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainorder.fragment.DeliveryOrderDetailFragment;
import com.chuanyang.driverh.responseresult.DeliverOrderData;
import com.chuanyang.driverh.responseresult.DeliverOrderResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.eventbus.RefleshDelieryEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import okhttp3.Call;


public class DeliveryOrderDetailActivity extends BaseTitleActivity {
    private Fragment mFragment;
    private DeliverOrderData deliverOrderData;
    private String waybillid;
    private int libraryId;
    private Dialog dialog;
    @Override
    protected int getContentResId() {

        return R.layout.base_single_fragment_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        Intent intent=getIntent();
        deliverOrderData = (DeliverOrderData) intent.getSerializableExtra("data");
        waybillid = intent.getStringExtra("waybillid");
        libraryId = intent.getIntExtra("libraryId",0);
        getTitleTextView().setText("出库详情");
        getRightTextView().setText("更新出库");
        setBackImg();

    }

    @Override
    protected void initData() {

        if (mFragment == null) {
            mFragment =  new DeliveryOrderDetailFragment();
        }
        getSupportFragmentManager().beginTransaction().add(R.id.single_fragment_content, mFragment).commit();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void baseGoEnsure() {
        String content="";
        if(deliverOrderData.getIsUpdate()==1) {
            content="这个出库清单已经更新过，是否还要强制更新?";
        }else {
            content="是否更新出库清单?";
        }
        dialog= DialogUtil.myDialog(activityContext, "更新出库单", "取消", "确定", content, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.dialog_btn_left:
                        break;
                    case R.id.dialog_btn_right:
                        updata();
                        break;
                }
                dialog.cancel();
            }
        });
    }
    private  void  updata()   {
        DialogUtil.showLoadingDialog(activityContext,"更新...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("waybillId",waybillid );
        params.put("libraryId", libraryId+"");
        MyHttpUtil.updateWaybillByLibraryId(activityContext,params, new MyCallback<DeliverOrderResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {

                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(DeliverOrderResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(activityContext,"更新成功！");
                    EventBus.getDefault().post(new RefleshDelieryEvent(true));
                    finish();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });



    }
}